package com.taru.order.dao;

import com.taru.order.entity.Answer;
import com.taru.order.entity.Category;
import com.taru.order.entity.Problem;
import com.taru.order.entity.User;
import com.taru.order.util.DbUtil;
import com.taru.order.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProblemDaoImpl {

    AnswerDaoImpl answerDao=new AnswerDaoImpl();
    UserDaoimpl userDaoimpl=new UserDaoimpl();

    /**
     * 添加问题
     */
    public int addProblem(Problem problem){
        String sql="insert into `tb_problem`(context,logo,user_id,create_time) values(?,?,now())";
        int code=DbUtil.update(sql,problem.getContext(),problem.getLogo(),problem.getUserId());
        return code;
    }
    /**
     * 获取最新的问题（列表）
     */
    public List<Problem> listProblem(Integer pageNum,Integer pageSize){
        String sql="select * from `tb_problem` order by create_time desc limit ?,?";
        List<Map<String,Object>> list = DbUtil.query(sql,(pageNum-1)*pageSize,pageSize);
        List<Problem> problemList=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                Problem problem=new Problem();
                problem.setId(StringUtil.valueOf(map.get("id")));
                problem.setContext(StringUtil.valueOf(map.get("context")));
                problem.setLogo(StringUtil.valueOf(map.get("logo")));
                problem.setCreateTime(StringUtil.valueOf(map.get("create_time")));
                problem.setModTime(StringUtil.valueOf(map.get("mod_time")));
                problem.setUserId(StringUtil.valueOf(map.get("user_id")));
                problem.setAnswers(answerDao.listAnswerOne(problem.getId()));
                problemList.add(problem);
            }
        }
        DbUtil.close();
        return problemList;

    }
    /**
     * 总共多少条
     */
    public Long queryToral(){
        String sql="SELECT count(*) FROM `tb_problem`";
        Long total=DbUtil.getTotalCount(sql);
        return total;
    }
    /**
     * 修改问题
     */
    public int modProblem(Problem problem){
        String sql="update `tb_problem` set context=?,logo=?,mod_time=now() where id=?";
        int code=DbUtil.update(sql,problem.getContext(),problem.getLogo(),problem.getId());
        return code;
    }
    /**
     * 删除问题
     */
    public int deleteProblem(List list) throws Exception {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            stringBuffer.append("\'");
            stringBuffer.append(list.get(i));
            stringBuffer.append("\',");
        }
        String sql = "delete from `tb_problem` where id in (" + stringBuffer.substring(0, stringBuffer.length() - 1) + ")";
        int code = 0;
        try {
            code = DbUtil.update(sql);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("DML异常");
        }
        DbUtil.close();
        return code;
    }
    /**
     * 获取问题详情（根据问题id，附带答案）
     */
    public Problem getProblem(String id){
        String sql="select * from `tb_problem` where id=?";
        List<Map<String,Object>> list = DbUtil.query(sql,id);
        Problem problem=null;
        if(list!=null && list.size()>0){
            Map<String,Object> map=list.get(0);
            problem=new Problem();
            problem.setId(StringUtil.valueOf(map.get("id")));
            problem.setLogo(StringUtil.valueOf(map.get("logo")));
            problem.setContext(StringUtil.valueOf(map.get("context")));
            problem.setCreateTime(StringUtil.valueOf(map.get("create_time")));
            problem.setModTime(StringUtil.valueOf(map.get("mod_time")));
            problem.setUserId(StringUtil.valueOf(map.get("user_id")));
            User user=userDaoimpl.userInfo(StringUtil.valueOf(map.get("user_id")));
            problem.setUser(user);
            problem.setAnswers(answerDao.listAnswer(problem.getId()));
        }
        DbUtil.close();
        return problem;

    }
}
